import time
import functools


def timing(name=None):
    def timer_decorator(func):
        @functools.wraps(func)
        def decorated(*args, **kwargs):
            start = time.time()
            ret = func(*args, **kwargs)
            if name is not None:
                func_name = name
            else:
                func_name = func.__name__
            print('{} use time: {:f}'.format(func_name, time.time() - start))
            return ret
        return decorated

    return timer_decorator


class Timer:
    def __init__(self, name=None):
        self.name = name
        self.start = 0

    def __enter__(self):
        self.start = time.time()

    def __exit__(self, type, value, traceback):
        print(self.name, 'use time:', time.time() - self.start)